# -*- coding: UTF-8 -*-

import os
import sys
import md5
import transaction

from sqlalchemy import engine_from_config

from pyramid.paster import (
    get_appsettings,
    setup_logging,
    )

from ..models.meta import (
    DBSession,
    Base,
    )
from ..models.models import (
    User,
    Role,
    Group,
    BusinessUser,
    ProductCategory
    )

from ..freedom import selectlist
from ..freedom.selectlist import (
    ZSelectList, 
    ZSelectListDetail,
    )

def usage(argv):
    cmd = os.path.basename(argv[0])
    print('usage: %s <config_uri>\n'
          '(example: "%s development.ini")' % (cmd, cmd)) 
    sys.exit(1)

def main(argv=sys.argv):
    if len(argv) != 2:
        usage(argv)
    config_uri = argv[1]
    setup_logging(config_uri)
    settings = get_appsettings(config_uri)
    engine = engine_from_config(settings, 'sqlalchemy.')
    DBSession.configure(bind=engine)
    Base.metadata.create_all(engine)
    with transaction.manager:
        #Add select list and detail
        selectlist_q = DBSession.query(ZSelectList)
        list_gender = ZSelectList(name=u'gender', description=u'性别')
        if selectlist_q.filter(ZSelectList.name == 'gender').count() == 0:
            DBSession.add(list_gender)
        list_subject = ZSelectList(name=u'subject', description=u'专业')
        if selectlist_q.filter(ZSelectList.name == 'subject').count() == 0:
            DBSession.add(list_subject)
        list_businesstype = ZSelectList(name=u'businesstype', description=u'商业类型')
        if selectlist_q.filter(ZSelectList.name == 'businesstype').count() == 0:
            DBSession.add(list_businesstype)
        list_coupontype = ZSelectList(name=u'coupontype', description=u'优惠券类别')
        if selectlist_q.filter(ZSelectList.name == 'coupontype').count() == 0:
            DBSession.add(list_coupontype)
        list_couponstatus = ZSelectList(name=u'couponstatus', description=u'优惠券状态')
        if selectlist_q.filter(ZSelectList.name == 'couponstatus').count() == 0:
            DBSession.add(list_couponstatus)
        list_productorderstatus = ZSelectList(name=u'productorderstatus', description=u'订单状态')
        if selectlist_q.filter(ZSelectList.name == 'productorderstatus').count() == 0:
            DBSession.add(list_productorderstatus)


        selectlistdetail_q = DBSession.query(selectlist.ZSelectListDetail)
        list_gender_detail_male = ZSelectListDetail(text=u'男', value=u'1', listname=list_gender)
        if selectlistdetail_q.filter(ZSelectListDetail.text == list_gender_detail_male.text).count() == 0:
            DBSession.add(list_gender_detail_male)
        list_gender_detail_female = ZSelectListDetail(text=u'女', value=u'2', listname=list_gender)
        if selectlistdetail_q.filter(ZSelectListDetail.text == list_gender_detail_female.text).count() == 0:
            DBSession.add(list_gender_detail_female)

        selectlistdetail_q = DBSession.query(selectlist.ZSelectListDetail)
        list_businesstype_detail_netstore = ZSelectListDetail(text=u'网店', value=u'1', listname=list_businesstype)
        if selectlistdetail_q.filter(ZSelectListDetail.text == list_businesstype_detail_netstore.text).count() == 0:
            DBSession.add(list_businesstype_detail_netstore)

        selectlistdetail_q = DBSession.query(selectlist.ZSelectListDetail)
        list_coupontype_detail_5ibang = ZSelectListDetail(text=u'5ibang优惠券', value=u'1', listname=list_coupontype)
        if selectlistdetail_q.filter(ZSelectListDetail.text == list_coupontype_detail_5ibang.text).count() == 0:
            DBSession.add(list_coupontype_detail_5ibang)
        selectlistdetail_q = DBSession.query(selectlist.ZSelectListDetail)
        list_coupontype_detail_ad = ZSelectListDetail(text=u'广告优惠券', value=u'2', listname=list_coupontype)
        if selectlistdetail_q.filter(ZSelectListDetail.text == list_coupontype_detail_ad.text).count() == 0:
            DBSession.add(list_coupontype_detail_ad)

        selectlistdetail_q = DBSession.query(selectlist.ZSelectListDetail)
        list_couponstatus_detail_draft = ZSelectListDetail(text=u'草稿', value=u'1', listname=list_couponstatus)
        if selectlistdetail_q.filter(ZSelectListDetail.text == list_couponstatus_detail_draft.text).count() == 0:
            DBSession.add(list_couponstatus_detail_draft)
        selectlistdetail_q = DBSession.query(selectlist.ZSelectListDetail)
        list_couponstatus_detail_wait = ZSelectListDetail(text=u'待审核', value=u'2', listname=list_couponstatus)
        if selectlistdetail_q.filter(ZSelectListDetail.text == list_couponstatus_detail_wait.text).count() == 0:
            DBSession.add(list_couponstatus_detail_wait)
        selectlistdetail_q = DBSession.query(selectlist.ZSelectListDetail)
        list_couponstatus_detail_online = ZSelectListDetail(text=u'在线', value=u'3', listname=list_couponstatus)
        if selectlistdetail_q.filter(ZSelectListDetail.text == list_couponstatus_detail_online.text).count() == 0:
            DBSession.add(list_couponstatus_detail_online)
        selectlistdetail_q = DBSession.query(selectlist.ZSelectListDetail)
        list_couponstatus_detail_offline = ZSelectListDetail(text=u'下线', value=u'4', listname=list_couponstatus)
        if selectlistdetail_q.filter(ZSelectListDetail.text == list_couponstatus_detail_offline.text).count() == 0:
            DBSession.add(list_couponstatus_detail_offline)
        selectlistdetail_q = DBSession.query(selectlist.ZSelectListDetail)
        list_couponstatus_detail_expired = ZSelectListDetail(text=u'已过期', value=u'5', listname=list_couponstatus)
        if selectlistdetail_q.filter(ZSelectListDetail.text == list_couponstatus_detail_expired.text).count() == 0:
            DBSession.add(list_couponstatus_detail_expired)


        selectlistdetail_q = DBSession.query(selectlist.ZSelectListDetail)
        list_productorderstatus_detail_new = ZSelectListDetail(text=u'下单', value=u'1', listname=list_productorderstatus)
        if selectlistdetail_q.filter(ZSelectListDetail.text == list_productorderstatus_detail_new.text).count() == 0:
            DBSession.add(list_productorderstatus_detail_new)
        selectlistdetail_q = DBSession.query(selectlist.ZSelectListDetail)
        list_productorderstatus_detail_cancel = ZSelectListDetail(text=u'取消', value=u'2', listname=list_productorderstatus)
        if selectlistdetail_q.filter(ZSelectListDetail.text == list_productorderstatus_detail_cancel.text).count() == 0:
            DBSession.add(list_productorderstatus_detail_cancel)
        selectlistdetail_q = DBSession.query(selectlist.ZSelectListDetail)
        list_productorderstatus_detail_delivered = ZSelectListDetail(text=u'送货', value=u'3', listname=list_productorderstatus)
        if selectlistdetail_q.filter(ZSelectListDetail.text == list_productorderstatus_detail_delivered.text).count() == 0:
            DBSession.add(list_productorderstatus_detail_delivered)
        selectlistdetail_q = DBSession.query(selectlist.ZSelectListDetail)
        list_productorderstatus_detail_income = ZSelectListDetail(text=u'收款', value=u'4', listname=list_productorderstatus)
        if selectlistdetail_q.filter(ZSelectListDetail.text == list_productorderstatus_detail_income.text).count() == 0:
            DBSession.add(list_productorderstatus_detail_income)
        selectlistdetail_q = DBSession.query(selectlist.ZSelectListDetail)
        list_productorderstatus_detail_completed = ZSelectListDetail(text=u'结束', value=u'5', listname=list_productorderstatus)
        if selectlistdetail_q.filter(ZSelectListDetail.text == list_productorderstatus_detail_completed.text).count() == 0:
            DBSession.add(list_productorderstatus_detail_completed)

        #Add admin role
        role_q = DBSession.query(Role)
        role_admin = Role(name=u'admin')
        if role_q.filter(Role.name == 'admin').count() == 0:
            DBSession.add(role_admin)
        role_normal = Role(name=u'normal')
        if role_q.filter(Role.name == u'normal').count() == 0:
            DBSession.add(role_normal)
        #Add admin group
        group_q = DBSession.query(Group)
        group = Group(name=u'admin')
        if group_q.filter(Group.name == u'admin').count() == 0:
            DBSession.add(group)
        #Add admin user
        user_q = DBSession.query(User)
        user_admin = User(name=u'admin', password=md5.new('admin').hexdigest())
        user_admin.role = role_admin
        user_admin.group = group
        if user_q.filter(User.name == u'admin').count() == 0:
            DBSession.add(user_admin)
        #Add test user
        user_q = DBSession.query(User)
        user_test = User(email=u'test@test.com', password=md5.new('test').hexdigest())
        user_test.role = role_normal
        user_test.group = group
        if user_q.filter(User.name == u'admin').count() == 0:
            DBSession.add(user_test)

        
        #add test business
        businessuser_q = DBSession.query(BusinessUser)
        businessuser_test = BusinessUser()
        businessuser_test.login = 'test@test.com'
        businessuser_test.password= md5.new('test').hexdigest()
        businessuser_test.companyname = u'我爱帮(5ibang.com)'
        businessuser_test.businesstype = 1
        businessuser_test.email = 'contact@5ibang.com'
        if businessuser_q.filter(BusinessUser.login == u'test').count() == 0:
            DBSession.add(businessuser_test)

        #add test product category
        category_q = DBSession.query(ProductCategory)
        category_test1 = ProductCategory()
        category_test1.name = u'图书'
        category_test1.priority = 0.1
        if category_q.filter(ProductCategory.name == u'图书').count() == 0:
            DBSession.add(category_test1)

        category_test2 = ProductCategory()
        category_test2.name = u'衣服'
        category_test2.priority = 0.2
        if category_q.filter(ProductCategory.name == u'衣服').count() == 0:
            DBSession.add(category_test2)

